import baseConfig from "./config";
import axios from 'axios';
axios.defaults.baseURL = baseConfig.baseUrl + ':' + baseConfig.port + baseConfig.prefix;

export default class http {
  static ajax_post(url, data, that, callback) {
  axios({
    method: "POST",
    headers: { 'Content-type': 'application/json', },
    url: axios.defaults.baseURL + url,
    data: data,
    //withCredentials:true
  }).then(function (res) {
    //alert('post:'+res)
    console.log(url + '\tPost请求到:');
    console.log(res);
    //alert('post-response:'+res);
    callback(that, res);
    //ajax_get('/manage/getinfo',this);
  }).catch(function (error) {
    alert('post失败')
    console.log(error);
  });
}
static ajax_get(url, that, callback) {
  axios({
    method: "GET",
    headers: { 'Content-type': 'application/json', },
    url: axios.defaults.baseURL + url,
    withCredentials: true
  }).then(function (res) {
    console.log(url + '\tGet请求到:')
    console.log(res);
    //alert('get:'+this.res);
    callback(that, res);

  }).catch(function (error) {
    alert('get下载失败')
    console.log(error);
  });
}
static ajax_post_params(url, data, that, callback = () => { }) {
  axios({
    method: 'post',
    url: axios.defaults.baseURL + url,
    headers: {
      'Content-type': 'application/x-www-form-urlencoded',
    },
    params: data,
  })
    .then(function (res) {
      //alert('post:'+res)
      console.log(url + '\tPost请求到:');
      console.log(res);
      //alert('post-response:'+res);
      callback(that, res);
      //ajax_get('/manage/getinfo',this);
    }).catch(function (error) {
      alert('post失败')
      console.log(error);
    });
}

}